<?php

namespace mpend\modules\app\models;

use Yii;
use common\helpers\Url;
use common\models\project\Project;
use common\models\project\ProjectPackage;

/**
 * 帮助数据模型
 *
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class PackageViewSearch extends ProjectPackage {

    public static function findByUdid($udid) {
        $query = static::find()->active(static::STATUS_ACTIVE)->andWhere(['udid' => $udid]);
        $query->innerJoinWith([
            'project project' => function ($q) {
                $q->active(Project::STATUS_ACTIVE);
            }
        ]);
        return $query->one();
    }

    public function getUrl() {
        $ext = pathinfo($this->link, PATHINFO_EXTENSION);
        if ($ext === 'plist') {
            return Url::itms($this->link);
        }
        return Url::attach($this->link);
    }

    public function getFilesize() {
        return Yii::$app->formatter->asShortSize($this->size, 2);
    }

    public function getUpdatetime() {
        return Yii::$app->formatter->asRelativeTime($this->updated_at);
    }

    public function getConsole() {
        if ($this->project->os == 'ios') {
            return [
                'text' => '点击安装',
                'tips' => '正在安装，请按HOME键返回桌面查看',
            ];
        } elseif ($this->project->os == 'android') {
            return [
                'text' => '下载安装',
                'tips' => '正在下载，请在浏览器中查看下载进度',
            ];
        }
        return null;
    }

    public function fields() {
        return [
            'id' => 'udid',
            'test' => function () {
                return Url::toShort($this->id);
            },
            'project',
            'type',
            'build',
            'version',
            'filesize',
            'console',
            'url',
            'qrcode',
            'updatetime'
        ];
    }

}
